
 /**  
 * Project Name:zhonghecrm  
 * File Name:FileController.java  
 * Package Name:com.crm.controller  
 * Date:2016年10月22日下午3:45:46   
 *  
*/  
  
package com.crm.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.crm.util.FdfsUtil;
import com.crm.util.PropertyReaderUtil;
import com.crm.util.ValidateUtil;

/**  
 * ClassName:FileController <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2016年10月22日 下午3:45:46 <br/>  
 * @author   hanlinxiu  
 * @version    
 * @since    JDK 1.8  
 * @see        
 */
@Controller
@RequestMapping("/file")
public class FileController {

	@RequestMapping("/uploadFile")
	@ResponseBody
	public Map<String, Object> uploadFile(HttpServletRequest request) throws Exception{
		
		Map<String, Object> map = new HashMap<>();
		map.put("error", 1);
		
		//定义允许上传的文件扩展名
		HashMap<String, String> extMap = new HashMap<String, String>();
		extMap.put("image", "gif,jpg,jpeg,png,bmp");
		
		//最大文件大小
		long maxSize = 1000000;
		
		if(!ServletFileUpload.isMultipartContent(request)){
			map.put("message", "请选择文件。");
			return map;
		}
		
		String dirName = request.getParameter("dir");
		if (dirName == null) {
			dirName = "image";
		}
		if(!extMap.containsKey(dirName)){
			map.put("message", "目录名不正确。");
			return map;
		}
		
		String files[] = {};
		
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Iterator<String> item = multipartRequest.getFileNames();
		while (item.hasNext()) {
			String fileName = item.next();
			MultipartFile file = multipartRequest.getFile(fileName);
			
			String originalFileName = file.getOriginalFilename();
			long fileSize = file.getSize();
			
			//检查文件大小
			if(fileSize > maxSize){
				map.put("message", "上传文件大小超过限制。");
				return map;
			}
			//检查扩展名
			String fileExt = originalFileName.substring(originalFileName.lastIndexOf(".") + 1).toLowerCase();
			if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
				map.put("message", "上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。");
				return map;
			}
			
			files = FdfsUtil.uploadFile(file, originalFileName, fileExt, fileSize);
			
			if(!ValidateUtil.isEmpty(files)){
				map.put("error", 0);
				map.put("url", PropertyReaderUtil.getValue("/config.properties", "fdfs_host") + files[0] + "/" + files[1]);
				return map;
			}else{
				map.put("message", "上传错误！");
			}
			
		}
		
		return map;
	}
	
}








  
